package com.runyun.project.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.util.StringUtil;
import org.springframework.util.DigestUtils;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class RunYunUtils {

    // 上传文件的跟目录
    public static final String ROOTPATH = "D:\\新建文件夹\\upload\\";

    // 生成随机字符串
    public static String generateUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    // MD5加密
    // hello -> abc123def456
    // hello + 3e4a8 -> abc123def456abc
    public static String md5(String key) {
        if (StringUtil.isEmpty(key)) {
            return null;
        }
        return DigestUtils.md5DigestAsHex(key.getBytes());
    }

    public static String getJSONString(int code, String msg, Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", code);
        json.put("msg", msg);
        if (map != null) {
            for (String key : map.keySet()) {
                json.put(key, map.get(key));
            }
        }
        return json.toJSONString();
    }

    public static String getJSONString(int code, String msg) {
        return getJSONString(code, msg, null);
    }

    public static String getJSONString(int code) {
        return getJSONString(code, null, null);
    }

    public static Map<String, Object> json2Map(String string) {
        Map<String, Object> map = new HashMap<String, Object>();
        map = (Map<String, Object>) JSON.parse(string);
        return map;
    }

    public static String dateToString(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(date);
    }

    public static String dateToString_(Date date) {
        if (date == null) return "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }

    public static Date stringToDate(String string) {
        if (string == null || "".equals(string)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(string);
        } catch (Exception e) {
            return new Date();
        }
    }

    public static Boolean copyFile2(String fromPath, String toPath) throws IOException {
        File fromFile = new File(fromPath);

        //如果文件不存在则创建文件
        if (!fromFile.exists()) {
            return false;
        }
        File goFile = new File(toPath);

        //创建输入流，读取fromFile
        InputStream in = new FileInputStream(fromFile);

        //创建输入流，写入到toFile
        OutputStream out = new FileOutputStream(goFile);

        byte[] buf = new byte[2048];
        int i = 0;
        while ((i = in.read(buf)) != -1) {
            out.write(buf, 0, i);
            out.flush();
        }
        out.close();
        in.close();
        System.out.println("文件复制结束！！！");
        return true;
    }

}
